/**
 * 
 */
package com.smarthome.web.property.controller.housemanager;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.housemanager.BuildingManagerService;
import com.smarthome.core.housemanager.BuildingUnitManagerService;
import com.smarthome.core.housemanager.ResidenceManagerService;
import com.smarthome.domain.bean.ShBuilding;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * @author dhc
 * @date 2015年9月14日 下午3:15:11
 * @title 小区楼栋管理
 */
@Controller
@RequestMapping("buildingUnitManager")
public class BuildingUnitController {
	
	private static Logger logger = LoggerFactory.getLogger(BuildingUnitController.class);
	
	@Resource(name="buildingUnitManagerService")
	private BuildingUnitManagerService buildingUnitManagerService;
	@Resource(name="buildingManagerService")
	private BuildingManagerService buildingManagerService;
	
	@Resource(name="residenceManagerService")
	private ResidenceManagerService residenceManagerService;
	boolean isAdmin= true;
	@RequestMapping("/views")
	public String views(HttpServletRequest request){
		isAdmin = UserHolder.isSuperResidence();
		//权限判断，如果是admin
		request.setAttribute("isAdmin", isAdmin);
		if(isAdmin){
			request.setAttribute("residences", residenceManagerService.queryShResidences(null));
		} else {
			Long residenceId = UserHolder.getCurrentResidenceId();
			ShResidence residence = new ShResidence();
			residence.setId(residenceId);
			//设置当前小区
			request.setAttribute("residence", residence);
			request.setAttribute("building", buildingManagerService.queryShBuildingsByResidenceId(residenceId));
		}
		
		return "/housemanager/buildingUnitManager";
	}
	
	
	@RequestMapping("/combobox")
	@ResponseBody
	public List<Combobox> getPage(Long parentId){
		return buildingUnitManagerService.getComboboxs(parentId);
	}
	
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShBuilding building, PageHelper page){
		return buildingUnitManagerService.getDataGrid(building, page);
	}
	
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request,Long pid) {
		request.setAttribute("residenceId", request.getParameter("residenceId"));
		request.setAttribute("parentId", request.getParameter("parentId"));
		//权限判断，如果是admin
		isAdmin = UserHolder.isSuperResidence();
		request.setAttribute("isAdmin", isAdmin);
		if(isAdmin){
			request.setAttribute("residentces", residenceManagerService.queryShResidences(null));
		} else {
			Long residenceId = UserHolder.getCurrentResidenceId();
			ShResidence residence = new ShResidence();
			residence.setId(residenceId);
			//设置当前小区
			request.setAttribute("residence", 	residenceManagerService.queryResidenceById(residenceId));
			request.setAttribute("building", buildingManagerService.queryShBuildingsByResidenceId(residenceId));
		}
		
        return "/housemanager/buildingUnitAdd";
	}
	@RequestMapping("/editPage")
	public String eidtPage(HttpServletRequest request,Long id) {
		ShBuilding bd = buildingUnitManagerService.querybuildingById(id);
		request.setAttribute("buildingUnit", bd);
		return "/housemanager/buildingUnitEdit";
	}
	
	@ResponseBody
	@RequestMapping("/add")
	public Result add(ShBuilding building){
		
		if(StringUtils.isBlank(building.getName())){
			return Result.failure("楼栋名字不能为空");
		}
		
		if(building.getParentId() == null){
			return Result.failure("请选择一个楼栋进行添加");
		}
		
		
		
		//重名验证
		if(buildingUnitManagerService.isRepeat(building)){
			return Result.failure("楼栋单元名称重复");
		}
		buildingUnitManagerService.add(building);
		return Result.success("添加楼栋单元成功");
	}
	
	
	@ResponseBody
	@RequestMapping("/edit")
	public Result edit(ShBuilding building){
		if(StringUtils.isBlank(building.getName())){
			return Result.failure("单元名字不能为空");
		}
		
		if(building.getParentId() == null){
			return Result.failure("请选择一个楼栋进行添加");
		}
		
		//重名验证
		if(buildingUnitManagerService.isRepeat(building)){
			return Result.failure("楼栋单元名称重复");
		}
		
		
		buildingUnitManagerService.edit(building);
		return Result.success("修改楼栋单元成功");
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(HttpServletRequest request,Long id){
		buildingUnitManagerService.delte(id);
		return Result.success("删除房屋楼栋单元成功");
	}
}
